package com.demo.demo.jinhua;

import java.util.Collections;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2022/7/8
 */
public class FgfGameService extends CardGameService implements Game {


    public FgfGameService(int playerNums) {
        // 定义房间的名称
        String roomCode = "FGF" + System.currentTimeMillis();
        this.setRoomCode(roomCode);
        this.setPlayerNums(playerNums);
        System.out.println("===========" + PLATFORM_NAME + "(" + PLATFORM_VERSION + ")" + "-" + FGF_NAME + "=========");
        System.out.println("房间号：" + this.getRoomCode() + " 玩家数量:" + this.getPlayerNums());
    }


    @Override
    public void shuffle() {
        super.shuffle();
        System.out.println("\n\n" + "============轮次(" + this.getRound() + ") 洗牌中==========");
        // 装牌
        for (String type : Poker.TYPES) {
            for (String point : Poker.POINTS) {
                String tp = type + point;
                Poker poker = new Poker(tp);
                cards.add(poker);
            }
        }
    // 洗牌
        Collections.shuffle(cards);
    }


    @Override
    public void deal() {
        int k = 0;
        for (int i = 1; i <= this.getPlayerNums(); i++) {
            String pn = i < 10 ? "0" + i : i + "";
            String playerName = "PLAYER" + pn;
            Poker poker1 = null;
            Poker poker2 = null;
            Poker poker3 = null;


            for (int j = 0; j < 3; j++) {
                if (j == 0) {
                    poker1 = (Poker) this.cards.get(k);
                }
                if (j == 1) {
                    poker2 = (Poker) this.cards.get(k);
                }
                if (j == 2) {
                    poker3 = (Poker) this.cards.get(k);
                }
                k++;
            }


            FgfCardHand cardHand = new FgfCardHand(playerName, poker1, poker2, poker3);
            playersCards.add(cardHand);
        }


// 打印玩家手牌信息
        for (CardHand hand : playersCards) {
            FgfCardHand fgfHand = (FgfCardHand) hand;
            System.out.println(fgfHand);
        }
    }


    @Override
    public void close() {
        System.out.println("\n\n" + "============轮次(" + this.getRound() + ") 结算==========");
        Collections.sort(this.playersCards);
// 打印结算完成后的玩家手牌信息
        int i = 0;
        for (CardHand hand : playersCards) {
            FgfCardHand fgfHand = (FgfCardHand) hand;

            int score = playersCards.size() - i++;

        // 取出当前账号的积分
            int currentScore = Player.PLAYERS.get(fgfHand.getPlayerName()).getScore();
            currentScore += score;
            Player.PLAYERS.get(fgfHand.getPlayerName()).setScore(currentScore);

            System.out.println(fgfHand);
        }


    }


}




